<template>
  <div class="file-list-container">
    <div class="file-list-header">
      <h2 class="title">
        <i class="fas fa-folder-open title-icon"></i>
        文件资源列表
      </h2>
      <div class="search-box">
        <div class="input-wrapper">
          <i class="fas fa-tasks input-icon"></i>
          <input
              type="text"
              v-model="taskName"
              placeholder="请输入任务名称"
              class="task-input"
              @keyup.enter="loadResources"
          >
        </div>
        <button @click="loadResources" class="load-btn">
          <i class="fas fa-search"></i> 查询文件
        </button>
      </div>
    </div>

    <div class="file-list-content">
      <div v-if="resources.length > 0" class="files-container">
        <div class="list-header">
          <span class="header-item">文件名称</span>
          <span class="header-action">操作</span>
        </div>
        <ul class="file-list">
          <li v-for="resource in resources" :key="resource.id" class="file-item">
            <div class="file-info">
              <i class="fas fa-file-alt file-icon"></i>
              <span class="file-name">{{ resource.resourceName }}</span>
            </div>
            <div class="file-actions">
              <router-link
                  :to="{ name: 'Preview', params: { fileName: resource.resourceName }}"
                  class="action-btn view-btn"
                  title="预览文件"
              >
                <i class="fas fa-eye"></i>
              </router-link>
              <a href="#" class="action-btn download-btn" title="下载文件">
                <i class="fas fa-download"></i>
              </a>
            </div>
          </li>
        </ul>
        <div class="list-footer">
          共 <span class="count-highlight">{{ resources.length }}</span> 个文件
        </div>
      </div>
      <div v-else class="empty-state">
        <div class="empty-icon-container">
          <i v-if="taskName" class="fas fa-folder-open empty-icon"></i>
          <i v-else class="fas fa-search empty-icon"></i>
        </div>
        <p v-if="resources.length === 0 && taskName" class="empty-text">
          没有找到相关文件，请尝试其他任务名称。
        </p>
        <p v-else class="empty-text">
          请输入任务名称以加载相关文件资源。
        </p>
        <button v-if="taskName" @click="taskName = ''" class="reset-btn">
          <i class="fas fa-redo-alt"></i> 重置搜索
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      taskName: '',
      resources: []
    };
  },
  methods: {
    async loadResources() {
      if (!this.taskName) {
        alert('请输入任务名称');
        return;
      }
      try {
        const response = await axios.get(`http://localhost:8080/api/resources/resources`, {
          params: { taskName: this.taskName }
        });
        this.resources = response.data;
      } catch (error) {
        console.error('加载文件列表失败:', error);
        alert('加载文件列表失败');
      }
    }
  }
};
</script>

<style scoped>
.file-list-container {
  max-width: 900px;
  margin: 0 auto;
  padding: 25px;
  background-color: #ffffff;
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.file-list-header {
  margin-bottom: 30px;
}

.title {
  color: #2c3e50;
  font-size: 26px;
  margin-bottom: 25px;
  padding-bottom: 12px;
  border-bottom: 2px solid #3498db;
  display: flex;
  align-items: center;
}

.title-icon {
  color: #3498db;
  margin-right: 12px;
}

.search-box {
  display: flex;
  gap: 12px;
}

.input-wrapper {
  flex: 1;
  position: relative;
}

.input-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #95a5a6;
}

.task-input {
  width: 100%;
  padding: 14px 15px 14px 45px;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  font-size: 15px;
  transition: all 0.3s;
  background-color: #f9f9f9;
}

.task-input:focus {
  border-color: #3498db;
  box-shadow: 0 0 8px rgba(52, 152, 219, 0.4);
  outline: none;
  background-color: #fff;
}

.load-btn {
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 6px;
  padding: 0 25px;
  cursor: pointer;
  font-weight: 500;
  font-size: 15px;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.load-btn:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.load-btn:active {
  transform: translateY(0);
}

.file-list-content {
  min-height: 250px;
}

.files-container {
  background-color: #f9f9f9;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.list-header {
  display: flex;
  justify-content: space-between;
  padding: 15px 20px;
  background-color: #edf2f7;
  color: #4a5568;
  font-weight: 600;
  border-bottom: 1px solid #e2e8f0;
}

.header-item {
  flex: 1;
}

.header-action {
  width: 100px;
  text-align: center;
}

.file-list {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

.file-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
  transition: all 0.2s;
}

.file-item:last-child {
  border-bottom: none;
}

.file-item:hover {
  background-color: #e9f7fe;
}

.file-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.file-icon {
  color: #3498db;
  margin-right: 12px;
  font-size: 18px;
}

.file-name {
  color: #2c3e50;
  font-weight: 500;
}

.file-actions {
  display: flex;
  gap: 10px;
}

.action-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  color: #718096;
  background-color: #edf2f7;
  transition: all 0.2s;
  text-decoration: none;
}

.view-btn:hover {
  background-color: #c6e6ff;
  color: #2b6cb0;
}

.download-btn:hover {
  background-color: #c6f6d5;
  color: #2f855a;
}

.list-footer {
  padding: 12px 20px;
  text-align: right;
  color: #718096;
  font-size: 14px;
  background-color: #edf2f7;
  border-top: 1px solid #e2e8f0;
}

.count-highlight {
  color: #3498db;
  font-weight: 600;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
  color: #7f8c8d;
}

.empty-icon-container {
  width: 80px;
  height: 80px;
  background-color: #edf2f7;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

.empty-icon {
  font-size: 36px;
  color: #a0aec0;
}

.empty-text {
  font-size: 16px;
  max-width: 320px;
  line-height: 1.6;
  margin-bottom: 20px;
}

.reset-btn {
  background-color: #edf2f7;
  color: #4a5568;
  border: none;
  border-radius: 6px;
  padding: 10px 16px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 6px;
}

.reset-btn:hover {
  background-color: #e2e8f0;
  color: #2d3748;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .file-list-container {
    padding: 20px 15px;
  }

  .search-box {
    flex-direction: column;
  }

  .load-btn {
    width: 100%;
    padding: 14px;
    margin-top: 10px;
  }

  .file-item {
    flex-direction: column;
    align-items: flex-start;
  }

  .file-actions {
    margin-top: 10px;
    align-self: flex-end;
  }

  .list-header {
    display: none;
  }
}

/* 添加一些动画效果 */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.file-item {
  animation: fadeIn 0.3s ease-out;
  animation-fill-mode: both;
}

.file-item:nth-child(1) { animation-delay: 0.05s; }
.file-item:nth-child(2) { animation-delay: 0.1s; }
.file-item:nth-child(3) { animation-delay: 0.15s; }
.file-item:nth-child(4) { animation-delay: 0.2s; }
.file-item:nth-child(5) { animation-delay: 0.25s; }
</style>